//
//  ColorUtils.swift
//  novelReader
//
//  Created by Ink on 2019/9/6.
//  Copyright © 2019 ChineseAll. All rights reserved.
//

import UIKit

extension UIColor {
    
    /// convenience color with hex
    ///
    /// - Parameter:
    ///     - hex: 6位 16进制
    ///     - alpha: 透明度 default = 1.0
    /// - Returns: UIColor 返回颜色
    public convenience init(_ hex: Int, alpha: CGFloat = 1.0) {
        
        self.init(red: CGFloat((hex & 0xff0000) >> 16) / 255,
                  green: CGFloat((hex & 0xff00) >> 8) / 255,
                  blue: CGFloat(hex & 0xff) / 255,
                  alpha: alpha)
    }
    
    /// convenience color with hexStr
    ///
    /// - Parameter:
    ///     - cStr: 6位 16进制字符串
    ///     - alpha: 透明度 default = 1.0
    /// - Returns: UIColor 返回颜色
    public convenience init(_ hexStr: String, alpha: CGFloat = 1.0) {
        let hexStr = hexStr.replacingOccurrences(of: "#", with: "")
        var hex :UInt32 = 0
        Scanner(string: hexStr).scanHexInt32(&hex)
        
        self.init(Int(hex), alpha: alpha)
    }
    
    /// 根据十六进制获取颜色
    /// - Parameter hex: 十六进制颜色
    /// - Returns: 返回颜色值
    public class func hexColor(_ hex: Int, alpha:CGFloat = 1.0) -> UIColor {
        return UIColor.init(hex, alpha: alpha)
    }
    
    
    /// 将UIColor转换为16进制字符串。
    public var hexString: String {
        
        let components = cgColor.components
        let r: CGFloat = components?[0] ?? 0.0
        let g: CGFloat = components?[1] ?? 0.0
        let b: CGFloat = components?[2] ?? 0.0
 
        let hexString = String(format: "#%02lX%02lX%02lX", lroundf(Float(r * 255)), lroundf(Float(g * 255)), lroundf(Float(b * 255)))
        return hexString
    }
}

extension UIColor {
    
    
    /// 获取两个中间色值
    /// - Parameters:
    ///   - startColor: 起始颜色
    ///   - endColor: 终点颜色
    ///   - franch: 取色位置
    /// - Returns: 取色值
    @objc public static func caculateColor(startColor: UIColor, endColor: UIColor, franch: CGFloat) -> UIColor {
        var startR : CGFloat = 0.0
        var startG : CGFloat = 0.0
        var startB : CGFloat = 0.0
        var startA : CGFloat = 0.0
        startColor.getRed(&startR, green: &startG, blue: &startB, alpha: &startA)
        
        var endR : CGFloat = 0.0
        var endG : CGFloat = 0.0
        var endB : CGFloat = 0.0
        var endA : CGFloat = 0.0
        endColor.getRed(&endR, green: &endG, blue: &endB, alpha: &endA)
        
        let r = (endR - startR) * franch + startR
        let g = (endG - startG) * franch + startG
        let b = (endB - startB) * franch + startB
        let a = (endA - startA) * franch + startA
        
        return UIColor(red: r, green: g, blue: b, alpha: a)
    }
}

public extension UIColor {
    static func random(alpha: CGFloat = 1.0) -> UIColor {
        return UIColor(
            red: CGFloat.random(in: 0...1),
            green: CGFloat.random(in: 0...1),
            blue: CGFloat.random(in: 0...1),
            alpha: alpha
        )
    }
}
